import { createRouter, createWebHistory } from 'vue-router'
import LoginView from '../views/LoginView.vue'
import HomeView from '../views/HomeView.vue'
import Logout from '../views/Logout.vue'

const routes = [
    { path: '/', redirect: '/login' },
    { path: '/login', component: LoginView },
    {
        path: '/home',
        component: HomeView,
        children: [
            { path: '', component: () => import('../views/SystemInfo.vue') },
            { path: 'vos', component: () => import('../views/VOSClient.vue') },
            { path: 'ip', component: () => import('../views/IPSettings.vue') },
            { path: 'blind', component: () => import('../views/BlindArea.vue') },
            { path: 'help', component: () => import('../views/Help.vue') },
            { path: 'quit', component: Logout },
        ]
    }
]

const router = createRouter({
    history: createWebHistory(),
    routes
})

router.beforeEach((to, from, next) => {
    const isAuthenticated = sessionStorage.getItem('isLogin')
    if (to.path !== '/login' && !isAuthenticated) next('/login')
    else next()
})

export default router